var num=0;
var checkedData=[];

jQuery(document).ready(function() {
	
	init();
	
	initq();
				
	//保存数据事件
	$('#save').click(function(){		
		for (var p = 0; p < num; p++) {
			getRadioBoxValue("question"+p);			
		}
		console.log(JSON.stringify(checkedData));
		$.ajax({ 
			method: 'post',
			url: basePath() + 'question/submit.do',
			data: JSON.stringify(checkedData),
			dataType: "json",
			contentType : 'application/json;charset=UTF-8',
			success: function(data){
				if(data.result=="1"){
					checkedData=[];	
					 $.confirm({
				            title: '提交成功',
				    	    content: '',
				            buttons: {
				                ok: {
				                    text: '查看结果',
				                    btnClass: 'btn-primary',
				                    keys: ['enter'],
				                    action: function(){
				        				window.location.href="../result/index.html?param="+$("#pk").val();
				                    }
				                },
				                cancel: {
				                    text: '取消',
				                    keys: ['esc']
				                }
				            }
				        })
				}
			}
		});	
	});
	
	$('#nosave').click(function(){
		window.location.href="../question/index.html";
	});
});		


function init(){
	//获取主键
	var pk=location.search.substring(1);
	pk=pk.replace('param=','');
	var da={};
	da.QUESTIONNAIREID=pk;
	da.pageNum="1";
	da.pageSize="10";
	$.ajax({ 
		type:"post",
		url: basePath() + 'question/list.do',
		data:JSON.stringify(da),
		dataType: "json",
		contentType : 'application/json;charset=UTF-8',
		success: function(data){			
			$("#pk").val(data.rows[0].QUESTIONNAIREID);
			$("#title").html(data.rows[0].QUESTIONNAIRETITLE);
			$("#disc").html(data.rows[0].QUESTIONNAIREDISC);			
      	}
	});
}


function initq() {
	//获取主键
	var pk=location.search.substring(1);
	pk=pk.replace('param=','');
	var da={};
	da.QUESTIONNAIREID=pk;
	da.pageNum="1";
	da.pageSize="10";
	$.ajax({ 
		type:"post",
		url: basePath() + 'question/qlist.do',
		data:JSON.stringify(da),
		dataType: "json",
		contentType : 'application/json;charset=UTF-8',
		success: function(data){	
			console.log(data);
			var questions = data.rows;
			var info="";
			num=questions.length;
			for (var i = 0; i < questions.length; i++) {
				var question=questions[i];
				info+='<div class="form-group"><div class="col-sm-1"></div><div class="col-sm-10"><hr style="height:1px;border:none;border-top:1px solid #555555;" />';
				if(question.QUESTIONTYPE=='1'){
					info+='<h4 id="'+question.QUESTIONID+'">'+question.QUESTION+'</h4>';
					var items=question.QUESTIONITEM;
					var item =items.split(",");
					for (var j = 0; j < item.length; j++) {
						info+='<input type="radio" value="'+item[j]+'" name="question'+i+'">'+item[j]+'</input><br>';
					}
				}else if(question.QUESTIONTYPE=='2'){
					info+='<h4 id="'+question.QUESTIONID+'">'+question.QUESTION+'(多选)</h4>';
					var items=question.QUESTIONITEM;
					var item =items.split(",");
					for (var j = 0; j < item.length; j++) {
						info+='<input type="checkbox" value="'+item[j]+'" name="question'+i+'">'+item[j]+'</input><br>';
					}
				}					
				info+='</div><div class="col-sm-1"></div></div>';
			}		
			$("#info").html(info);
      	}
	});
}

function  getRadioBoxValue(radioName) { 
      var obj = $("[name="+radioName+"]");  //这个是以标签的name来取控件
      for(i=0; i<obj.length;i++){
             if(obj[i].checked){ 
            	var data={}
                var pk=obj.prev("h4").attr("id");
                var item=obj[i].value;
                data.QUESTIONNAIREID=$("#pk").val();
                data.QUESTIONID=pk;
                data.QUESTIONITEM=item;
                checkedData.push(data);
              } 
        }               
 } 




